//index.js
//获取应用实例
const app = getApp();
Page({
      data: {
            title: [],
            list: [],
            sl: {}
      },
      toDetail: function(event) {
            const id = event.currentTarget.dataset.id;
            console.log(event)
            wx.navigateTo({
                  url: '/pages/detail/detail?id=' + id,
            })
      },
      onLoad: function() {
            var that = this;
            var arr = [];
            var arr1 = [];
            wx.request({
                  url: 'http://localhost:8080/v2/movie/coming_soon?count=3', //仅为示例，并非真实的接口地址
                  success(res) {
                        res.data.subjects.forEach(function(item) {
                              const arr2 = [];
                              var num = parseInt(Number(item.rating.stars) / 10);
                              for (var i = 0; i < 5; i++) {
                                    if (i < num) {
                                          arr2.push(1)
                                    } else {
                                          arr2.push(0)
                                    }
                              }
                              item.newstar = arr2;
                        })
                        arr.unshift(res.data.title);
                        arr1.unshift(res.data.subjects);
                        that.setData({
                              title: arr,
                              list: arr1
                        })
                  }
            })
            wx.request({
                  url: 'http://localhost:8080/v2/movie/in_theaters?count=3', //仅为示例，并非真实的接口地址
                  success(res) {
                        console.log(res.data)
                        res.data.subjects.forEach(function(item) {
                              const arr2 = [];
                              var num = parseInt(Number(item.rating.stars) / 10);
                              for (var i = 0; i < 5; i++) {
                                    if (i < num) {
                                          arr2.push(1)
                                    } else {
                                          arr2.push(0)
                                    }
                              }
                              item.newstar = arr2;
                        })
                        arr.unshift(res.data.title);
                        arr1.unshift(res.data.subjects);
                        that.setData({
                              title: arr,
                              list: arr1
                        })
                  }
            })
            wx.request({
                  url: 'http://localhost:8080/v2/movie/top250?count=3', //仅为示例，并非真实的接口地址
                  success(res) {
                        console.log(res.data)
                        res.data.subjects.forEach(function(item) {
                              const arr2 = [];
                              var num = parseInt(Number(item.rating.stars) / 10);
                              for (var i = 0; i < 5; i++) {
                                    if (i < num) {
                                          arr2.push(1)
                                    } else {
                                          arr2.push(0)
                                    }
                              }
                              item.newstar = arr2;
                        })
                        arr.unshift(res.data.title);
                        arr1.unshift(res.data.subjects);
                        that.setData({
                              title: arr,
                              list: arr1
                        })
                  }
            })
      },
      toMore: function(event) {
            const t = event.target.dataset.name
            wx.navigateTo({
                  url: '/pages/more/more?title=' + t,
            })
      }

})